package com.crm.customers.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.filter.AutoLoad;
import com.crm.customers.dao.CustomerDao;
import com.crm.customers.dao.CustomerInfoTempDao;
import com.crm.customers.model.CustomerInfo;
import com.crm.customers.model.CustomerInfoTemp;
import com.crm.customers.service.CustomerInfoTempService;
import com.crm.sys.dao.SysDao;
import com.crm.sys.model.Log;
import com.crm.user.dao.UserDao;
import com.crm.user.model.UserInfo;
import com.crm.util.Constants;
import com.wjyup.coolq.util.service.impl.BaseService;

@Service("customerInfoTempService")
public class CustomerInfoTempServiceImpl extends BaseService implements CustomerInfoTempService {

	@Autowired
	private CustomerInfoTempDao customerInfoTempDao;
	
	@Autowired
	private CustomerDao customerDao;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private SysDao sysDao;
	
	@Override
	public int insertTempInfo(CustomerInfoTemp cit) {
		return customerInfoTempDao.insert(cit);
	}

	@Override
	public List<CustomerInfoTemp> getAllCustomerInfoTempList(
			CustomerInfoTemp cit, Integer pageNo, Integer pageSize) {
		if (pageNo != null && pageSize != null) {
			cit.setStartsize((pageNo-1)*pageSize);
			cit.setEndsize(pageSize);
		}
		List<CustomerInfoTemp> list = customerInfoTempDao.getAllCustomerInfoTempList(cit);
		return list;
	}

	@Override
	public int bindCustomer(CustomerInfoTemp cit,Long loginuid) {
		String tel = cit.getTel();
		//因为以前导入的资源很多重复的,所以结果用list接收
		CustomerInfo ci = new CustomerInfo();
		ci.setTel(tel);
		Long cusid = null;
		List<CustomerInfo> list = customerDao.getCusListByTel(ci);
		if (list != null && list.size() > 0){
			//获取客户对象
			ci = list.get(0);
			//获取客户id
			cusid = ci.getId();
			//判断客户是否在别人库中,如果在别人库中,不获取,如果在公共库,获取到自己库中
			if (ci.getState() == 4) {
				//在公共库或原本就在我库中,获取到意向不明库中
				ci.setState(1);
				ci.setUid(cit.getUid());
				ci.setCreateuser(cit.getUid());
				//更新客户信息
				ci = customerDao.updateCustomerInfo(ci);
				//存入log
				Log log = new Log();
				log.setContentid(ci.getId());
				log.setCategory(Constants.LOGCATEGORY.ZERO);
				log.setState(Constants.LOGSTATE.FIVE);
				log.setOid(ci.getUid());
				UserInfo ui= new UserInfo();
				ui.setUid(ci.getUid());
				ui = userDao.getUserInfo(ui);
				log.setDescription("分配客户"+ ci.getCname() +"给"+ui.getName());
				log.setCreateuser(loginuid);
				log.setUid(loginuid);
				sysDao.insertLog(log);
				//获取到我的库,给一个标记
				cit.setOwntype(0);
			} else if (ci.getUid() == cit.getUid()) {
				//在自己库中,不用修改
				cit.setOwntype(0);
			} else {
				//在别人的库中,无法获取,给一个标记
				cit.setOwntype(1);
			}
		} else {
			//说明没有这个客户,给个标记
			cit.setOwntype(-1);
		}
		cit.setCusid(cusid);
		//更新临时表
		int result = customerInfoTempDao.update(cit);
		return result;
	}

	@Override
	public CustomerInfoTemp getCusInfoTempById(Long id) {
		return customerInfoTempDao.getCusInfoTempById(id);
	}

	@Override
	public int delete(CustomerInfoTemp c) {
		return customerInfoTempDao.delete(c);
	}

	@Override
	public List<CustomerInfoTemp> getListByParams(CustomerInfoTemp c,
			Integer pageNo, Integer pageSize) {
		c.setStartsize((pageNo-1)*pageSize);
		c.setEndsize(pageSize);
		return customerInfoTempDao.getListByParams(c);
	}

	@Override
	public int update(CustomerInfoTemp c) {
		return customerInfoTempDao.update(c);
	}

	@Override
	public int bindCustomer(String tel, Long cusid, Long uid) {
		CustomerInfoTemp cit = new CustomerInfoTemp();
		cit.setTel(tel);
		//担心有重复资源
		List<CustomerInfoTemp> list = customerInfoTempDao.getListByParams(cit);
		for (CustomerInfoTemp c : list) {
			//绑定资源,并分配给添加的这个人
			c.setUid(uid);
			c.setCusid(cusid);
			c.setOwntype(0);
			customerInfoTempDao.update(c);
		}
		return 0;
	}

	@Override
	public int checkTelIsExist(CustomerInfoTemp c) {
		return customerInfoTempDao.checkTelIsExist(c);
	}
	
	
	
}
